package io.afu.validator.Implement;

import io.afu.validator.Annimation.ChineseWord;
import io.afu.validator.Annimation.LimitedOnly;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class LimitedOnlyImpl implements ConstraintValidator<LimitedOnly,String> {

    private String[] strs;

    private Boolean allowEmpty;

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (null == s || "".equals(s)){
            return this.allowEmpty;
        }else {
            for (String str:this.strs){
                if (s.equals(str)){
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void initialize(LimitedOnly constraintAnnotation) {
        this.strs = constraintAnnotation.limit();
        this.allowEmpty = constraintAnnotation.allowEmpty();
    }
}
